package runInfo;

import java.io.IOException;
import java.text.DecimalFormat;

import org.eclipse.swt.widgets.Display;

import com.phidgets.PhidgetException;
import com.phidgets.TemperatureSensorPhidget;

import runInfo.TestThreadGUI;

public class TestThreadTemp implements Runnable {


	boolean stopping = false;
	Thread currentThread = null;
	
	@Override
	public void run() {
		long maxRunTime = 1000;						// ms
		maxRunTime = 1000*maxRunTime;				// s
		long startTime = System.currentTimeMillis();
		long currentTime = System.currentTimeMillis();			
		TemperatureSensorPhidget tempsensor;
		try {
			tempsensor = new TemperatureSensorPhidget();
			tempsensor.openAny();
			System.out.println("waiting for new thread TemperatureSensor attachment...");
			tempsensor.waitForAttachment();
			tempsensor.setTemperatureChangeTrigger(0, 0.2);
			tempsensor.setThermocoupleType(0, 4);
			while(Data.programRunning){
				currentTime = System.currentTimeMillis();
			    Data.timeS 	= String.valueOf(((currentTime-startTime)/1000.0));
			    GetTemps(tempsensor, startTime);
				System.out.println("temp = " + Data.tempTC + "measuring temp for: " + Data.timeS + " s");
				System.out.println("program running? " + Data.programRunning); 
				try{
					Thread.sleep((long) 1000);			// sleep for X ms
				}catch(InterruptedException e){
				}
			}			
		} catch (PhidgetException e2) {
			e2.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("No longer measureing temperature ------------------------------------");
		
	}
	

	public void GetTemps(TemperatureSensorPhidget tempsensor, Long startTime ) throws PhidgetException, IOException{
		double calibration = 0.3;
		Data.tempTC = tempsensor.getTemperature(0)-calibration;
		Data.tempAC = tempsensor.getAmbientTemperature()-calibration;
		Data.tempTF = Data.tempTC*9/5 + 32;
		Data.tempAF = Data.tempAC*9/5 + 32;	//			System.out.println("finished acquiring temps");

		DecimalFormat df = new DecimalFormat("###.###");
		Data.tempTC = Double.valueOf(df.format(Data.tempTC));
		Data.tempAC = Double.valueOf(df.format(Data.tempAC));
		Data.tempTF = Double.valueOf(df.format(Data.tempTF));
		Data.tempAF = Double.valueOf(df.format(Data.tempAF));

		//		System.out.println("----------HERE-----------");

		double runningTime = (1.0/1000.0)*(1.0/60.0)*(System.currentTimeMillis()-startTime);				
		Data.timeS = String.valueOf(runningTime); 	
		TestThreadGUI.WriteData(runningTime);
	
		Display.getDefault().asyncExec(new Runnable() {
		 public void run() {
			 TestThreadGUI.tempTextC.setText(String.valueOf(Data.tempTC));
			 TestThreadGUI.tempTextF.setText(String.valueOf(Data.tempTF));
			 TestThreadGUI.temptextAC.setText(String.valueOf(Data.tempAC));
			 TestThreadGUI.temptextAF.setText(String.valueOf(Data.tempAF));
			 Data.corrAbvS = TestThreadGUI.corrAbvText.getText();
			 Data.measAbvS = TestThreadGUI.measAbvText.getText();
			 Data.boilTempS = TestThreadGUI.boilerTempCombo.getText();
			 Data.distTemp = TestThreadGUI.distTempCombo.getText();
		}
	});
}
	


}
